﻿using STT.Business;
using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace STT.Profile
{
    public partial class Default : System.Web.UI.Page
    {
        ToolBox tb = new ToolBox();
        Control ct = new Control();
        Control menu = new Control();
        string module = "";
        string key = "";

        protected void Page_Load(object sender, EventArgs e)
        {
 
            if (Request.Cookies["UserinfoFDN"] != null)
            {
                LoadSessionbycookie();
            }
            if (Session["roleFhvgv"] != null && Session["TenFDN"] != null)
            {
                HtmlGenericControl gel2 = new HtmlGenericControl("div");
                if (Session["roleFhvgv"].ToString() == "4" && Session["MaFhv"] != null)
                {
                    DataTable dt = tb.GetDataTableById2("Get_HocVien_ByMaHV", Session["MaFhv"].ToString().Trim());
                    DataTable dttnhv = tb.GetDataTableById2("TbTinNhan_HVTTTN", Session["MaFhv"].ToString().Trim());
                    lbltenhv.Text = Session["TenFDN"].ToString();
                    lblsothongbao.Text = dt.Rows[0]["TrangThaiTB"].ToString().Trim();
                    menu = Page.LoadControl("~/Profile/UserControlsHV/UcMenuHV.ascx");
                    dmk.HRef = "Default.aspx?mod=DMK1303";
                    ttcn.HRef = "Default.aspx?mod=TTCN117";
                    //xemtn.HRef = "Default.aspx?mod=CTTNHV1411";
                    xemtn.HRef = "#";
                    txttimkiem.Attributes.Add("placeholder", "Gõ từ khóa muốn tìm kiếm theo giảng viên....");
                    LoadTBHV();
                    LoadTNHV();
                    lbltinnhan.Text = dttnhv.Rows[0]["TNHV"].ToString();

                }
                else if (Session["roleFhvgv"].ToString() == "5" && Session["MaFgv"] != null)
                {
                    ct = Page.LoadControl("UserControlsGV/UcCongViec.ascx");
                    pl_CongViec.Controls.Add(ct);
                    DataTable dt2 = tb.GetDataTableById2("GiangVien_GetByMagv", Session["MaFgv"].ToString().Trim());
                    DataTable dttngv = tb.GetDataTableById2("TbTinNhan_GVTTTN", Session["MaFgv"].ToString().Trim());
                    lblsothongbao.Text = dt2.Rows[0]["TrangThaiTB"].ToString().Trim();
                    lbltenhv.Text = Session["TenFDN"].ToString();
                    dmk.HRef = "Default.aspx?mod=DMKGV1303";
                    ttcn.HRef = "Default.aspx?mod=TTCNGV117";
                    //xemtn.HRef = "Default.aspx?mod=CTTNGV1411";
                    xemtn.HRef = "#";
                    menu = Page.LoadControl("~/Profile/UserControlsGV/UcMenuGV.ascx");
                    txttimkiem.Attributes.Add("placeholder", "Gõ từ khóa muốn tìm kiếm theo học viên....");
                    LoadTBGV();
                    LoadTNGV();
                    lbltinnhan.Text = dttngv.Rows[0]["TNGV"].ToString();
                }
                if (Session["TenFDN"].ToString().Contains("GV"))
                {

                    lvDanhSachChat.DataSource = BTbDangNhap.SelectAllChatForGiangVien(Session["TenFDN"].ToString());
                    lvDanhSachChat.DataBind();
                }
                else
                {
                    lvDanhSachChat.DataSource = BTbDangNhap.SelectAllChatForHocVien(Session["TenFDN"].ToString());
                    lvDanhSachChat.DataBind();
                }
                gel2.Controls.Add(menu); pl_menu.Controls.Add(gel2);
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
            HtmlGenericControl gel = new HtmlGenericControl("div");
            if (Request.QueryString["mod"] == null)
            {
                Response.Redirect("Default.aspx?mod=Home");
            }
            else
            {
                module = Request.QueryString["mod"].ToString().Trim();
                if (module == "Home")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                        ct = Page.LoadControl("UserControlsHV/UcIndex.ascx");
                    else if (Session["roleFhvgv"].ToString() == "5")
                        ct = Page.LoadControl("UserControlsGV/UcIndex.ascx");

                }
                else if (module == "TTKH1411")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                        ct = Page.LoadControl("UserControlsHV/UcThongTinKhoaHoc.ascx");
                    else
                        Response.Redirect("~/Default.aspx");

                }
                else if (module == "CTTNHV1411")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                        ct = Page.LoadControl("UserControlsHV/UcChiTietTinNhan.ascx");
                    else
                        Response.Redirect("~/Default.aspx");

                }

                else if (module == "QLNV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                        ct = Page.LoadControl("UserControlsHV/UcQuanLyNhanVien.ascx");
                    else
                        Response.Redirect("~/Default.aspx");

                }
                else if (module == "TTCN117")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {

                        ct = Page.LoadControl("UserControlsHV/UcThongTinCaNhan.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "DHV132")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {

                        ct = Page.LoadControl("UserControlsHV/UcBangDiem.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "TLHTHV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {

                        ct = Page.LoadControl("UserControlsHV/UcTaiLieuHocTap.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "TTGV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {

                        ct = Page.LoadControl("UserControlsHV/UcThongTinGiangVien.ascx");

                    }
                    else Response.Redirect("~/Default.aspx");

                }
                else if (module == "TKB1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {
                        ct = Page.LoadControl("UserControlsHV/UcThoiKhoaBieu.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");


                }
                else if (module == "DMK1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {
                        ct = Page.LoadControl("UserControlsHV/UcDoiMatKhau.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "TKGVHV132")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {
                        ct = Page.LoadControl("UserControlsHV/UcTimKiem.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "DSTBHV1707")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {
                        ct = Page.LoadControl("UserControlsHV/UcThongBaoHV.ascx");
                    }
                    else if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcThongBaoGV.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "CTTBHV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "4")
                    {
                        key = Request.QueryString["key"].ToString().Trim();
                        SqlDataReader dr;
                        SqlConnection cn = new SqlConnection(tb.str);
                        tb.OpenConnection(cn);
                        SqlCommand cmd1 = new SqlCommand("[GetId_TinTuc]", cn);
                        cmd1.CommandType = CommandType.StoredProcedure;

                        dr = cmd1.ExecuteReader();

                        while (dr.Read())
                        {
                            if (key == dr[0].ToString().Trim())
                            {
                                ct = Page.LoadControl("UserControlsHV/UcChiTietThongBaoHV.ascx");
                            }
                        }
                        dr.Close();
                        //ct = Page.LoadControl("UserControlsHV/UcThongBaoHV.ascx");
                    }
                    else if (Session["roleFhvgv"].ToString() == "5")
                    {
                        key = Request.QueryString["key"].ToString().Trim();
                        SqlDataReader dr;
                        SqlConnection cn = new SqlConnection(tb.str);
                        tb.OpenConnection(cn);
                        SqlCommand cmd2 = new SqlCommand("[GetId_TinTuc]", cn);
                        cmd2.CommandType = CommandType.StoredProcedure;

                        dr = cmd2.ExecuteReader();

                        while (dr.Read())
                        {
                            if (key == dr[0].ToString().Trim())
                            {
                                ct = Page.LoadControl("UserControlsGV/UcChiTietThongBaoGV.ascx");
                            }
                        }
                        dr.Close();
                        //ct = Page.LoadControl("UserControlsHV/UcThongBaoHV.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "TTCNGV117")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcThongTinCaNhan.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "QLDHV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcQuanLyDiem.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "GTLHVGV132")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcGuiTaiLieuHocVien.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "DSHVGV117")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcDanhSachHocVien.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "LGDGV1411")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcLichGiangDay.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "DMKGV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcDoiMatKhau.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "TTHV1303")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcDanhSachHocVien.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                else if (module == "CTTNGV1411")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                        ct = Page.LoadControl("UserControlsGV/UcChiTietTinNhan.ascx");
                    else
                        Response.Redirect("~/Default.aspx");

                }
                else if (module == "TKHVGV132")
                {
                    if (Session["roleFhvgv"].ToString() == "5")
                    {
                        ct = Page.LoadControl("UserControlsGV/UcTimKiem.ascx");
                    }
                    else Response.Redirect("~/Default.aspx");
                }
                gel.Controls.Add(ct); plindex.Controls.Add(gel);

            }
           
        }
        public void LoadSessionbycookie()
        {
            try
            {
                DataTable dt = tb.GetDataTableById2("[GetTkDangNhapByTenDN]", Request.Cookies["UserinfoFDN"]["Username"]);
                if (dt.Rows[0]["Quyen"].ToString().Trim() == "4")
                {
                    DataTable hv = tb.GetDataTableById2("[Get_HocVien_ByMaHV]", Request.Cookies["UserinfoFDN"]["Username"]);
                    Session["roleFhvgv"] = "4";
                    Session["MaFhv"] = Request.Cookies["UserinfoFDN"]["Username"];
                    Session["TenFDN"] = Request.Cookies["UserinfoFDN"]["Username"];
                    Session["FAnh"] = hv.Rows[0]["Anh"].ToString();
                    Session["Chucvu"] = hv.Rows[0]["TenChucVu"].ToString();
                    //Response.Redirect("~/Profile/Default.aspx");
                }
                else if (dt.Rows[0]["Quyen"].ToString().Trim() == "5")
                {
                    DataTable gv = tb.GetDataTableById2("[GiangVien_GetByMagv]", Request.Cookies["UserinfoFDN"]["Username"]);
                    Session["roleFhvgv"] = "5";
                    Session["MaFgv"] = Request.Cookies["UserinfoFDN"]["Username"];
                    Session["TenFDN"] = Request.Cookies["UserinfoFDN"]["Username"];
                    Session["FAnh"] = gv.Rows[0]["Anh"].ToString();
                    //Session["MaFhv"] = username.Text;
                    //Response.Redirect("~/Profile/Default.aspx");
                }
            }
            catch { }
        }
        private void LoadTBHV()
        {
            ddlthongbao.DataSource = tb.GetDataTable("TbTinTuc_SelectTop");
            ddlthongbao.DataBind();
        }
        private void LoadTBGV()
        {
            ddlthongbao.DataSource = tb.GetDataTable("TbTinTuc_GVSelectTop");
            ddlthongbao.DataBind();
        }
        private void LoadTNHV()
        {
            ddltinnhan.DataSource = tb.GetDataTableById2("TbTinNhan_HVSelectTop", Session["MaFhv"].ToString());
            ddltinnhan.DataBind();
        }
        private void LoadTNGV()
        {
            ddltinnhan.DataSource = tb.GetDataTableById2("TbTinNhan_GVSelectTop", Session["MaFgv"].ToString());
            ddltinnhan.DataBind();
        }
        public string DisplayImage(string TenDN, string Anh)
        {
            string str = "";


            str = Anh == "" ? "../Uploads/Avatar/avatar-default.jpg" : "../Uploads/Avatar/" + Anh;

            return str;
        }
        protected void lbtthoat_Click(object sender, EventArgs e)
        {
            Session["MaFgv"] = null;
            Session["roleFhvgv"] = null;
            Session["MaFhv"] = null;
            Session["TenFDN"] = null;
            Response.Cookies["UserinfoFDN"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect("~/Default.aspx");
        }
        protected void ImgbtTk_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["roleFhvgv"].ToString() == "4" && Session["MaFhv"] != null)
            {
                Response.Redirect("Default.aspx?mod=TKGVHV132&key=" + txttimkiem.Text);
            }
            else if (Session["roleFhvgv"].ToString() == "5" && Session["MaFgv"] != null)
            {
                Response.Redirect("Default.aspx?mod=TKHVGV132&key=" + txttimkiem.Text);
            }
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        [WebMethod]
        public static void UpdateTBHV(string MaHV, string Rolehvgv)
        {
            if (Rolehvgv == "4")
            {
                ETbHocVien hv = new ETbHocVien();
                hv.MaHV = MaHV;
                hv.TrangThaiTB = 0;
                BTbHocVien.UpdateTBHV(hv);

            }
            else if (Rolehvgv == "5")
            {
                ETbGiangVien gv = new ETbGiangVien();
                gv.MaGV = MaHV;
                gv.TrangThaiTB = 0;
                BTbGiangVien.UpdateTBGV(gv);
            }
        }
        [WebMethod]
        public static void UpdateTNHVGV(string MaHVGV, string Rolehvgv)
        {
            if (Rolehvgv == "4")
            {
                ETbHocVien hv = new ETbHocVien();
                hv.MaHV = MaHVGV;
                BTbHocVien.UpdateTNHV(hv);
            }
            else if (Rolehvgv == "5")
            {
                ETbGiangVien gv = new ETbGiangVien();
                gv.MaGV = MaHVGV;
                BTbGiangVien.UpdateTNGV(gv);
            }
        }

        string mark = "";
        protected void lvDanhSachChat_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e)
        {
            string str = " ";
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                DataRowView rowView = e.Item.DataItem as DataRowView;

                if (rowView["TenDN"].ToString().Trim().Substring(0, 2) != mark)
                {
                    mark = rowView["TenDN"].ToString().Substring(0, 2);
                    if (mark == "HV")
                    {
                        if (Session["TenFDN"].ToString().Contains("HV"))
                            str = "Học viên cùng lớp";
                        else
                            str = "Cán bộ lớp";

                    }
                    else
                        str = "Giảng viên giảng dạy";
                    e.Item.Controls.AddAt(0, new Literal() { Text = "<div class='date_spend'>" + str + "</div>" });
                }
            }
        }
    }
}